<template>
	<view >
		<view style=" display: flex;justify-content: center;
		background-color: #CFEAF1;height: 44px;width: 100%;align-items: center;
		position: sticky;top: 0;z-index: 1;">
			<image @click="tohome()" src="../../static/back.png" style="height: 30px;width: 30px;
		position: absolute;left: 10px;"></image>
			<h3>食物详情</h3>
		</view>
	<view style="display: flex;flex-direction: column;justify-content: center;align-items: center;">
		<image :src="foodone.image" style="width: 100%;"></image>
		<view class="" style="display: flex;height: 40px;align-items: center;
		width: 90%;margin:15px">
			<text style="font-size: 20px;line-height: 40px;margin-left: 10px;
			display: block;margin-top: 10px;width: 150px;">{{foodone.foodname}}</text>
			<button type="primary" style="border-radius: 50px;margin-left: 50px;
			line-height: 40px;" @click="tocomment(foodone.foodId)">查看评论</button>
			</view>

		<view class="" style="width: 90%;display: block;text-indent: 2em;">
			<text space="nbsp">{{foodone.content}}</text>
		</view>

		<text style="margin-left: 200px;margin-top: 20px;font-size: 20px;">￥{{foodone.price}}</text>

		<view class="" style="height: 100px;">

		</view>

		<view class="" style="width: 100%;height: 70px;background-color: #CFEAF1;
		                      position: fixed;bottom: 0;display: flex;">
			<uni-icons class="lh" type="minus" size="35px" @click="min"></uni-icons>
			<text class="lh">{{sum}}</text>
			<uni-icons class="lh" type="plus" size="35px" @click="add"></uni-icons>
			<text class="lh">共计：￥{{numfilter(foodone.price*sum)}}</text>
			<button type="primary" style="border-radius: 50px;
			height: 40px;line-height: 40px;margin-top: 15px;
			width: 100px;position: fixed;right: 20px;" @click="submit">提交订单</button>
		</view>
		


	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				foodone: {},
				sum: 1,
				allorder: [],
				noPay: true,
				id: '',
				total:'',
				user: true,
				oneuser: {},
				foodid:''

			}
		},
		methods: {
			tohome(){
				uni.switchTab({
					url:'/pages/tab/home'
				})
			},
			tocomment(foodId){
				uni.navigateTo({
					url:'/pages/commentlist/commentlist?foodId='+foodId
				})
			},
			numfilter(value) {
				// value = this.foodone.price*this.sum
				var total = parseFloat(value).toFixed(2)
				this.total=total
				return total
			},
			min() {
				if (this.sum == 1) {
					uni.showToast({
						title: '不能减少了',
						icon: 'none'
					});
				} else {
					this.sum--
				}
			},
			add() {

				this.sum++
			},
			submit() {
				
				if (!this.oneuser) {
					uni.switchTab({
						url:'/pages/tab/my',
						
					})
					// 表格无订单时，创建
			}else {
			if (this.allorder.length == 0) {
					uni.request({
						url: 'http://localhost:8484/ttwm/ttwm/addOrder',
						method: 'POST',
						data: {
							"userId": this.oneuser.userId,
							"payment": '未支付'
						},
						success: res => {
							if (res.data.success) {
								uni.request({
									url: 'http://localhost:8484/ttwm/ttwm/allOrder',
									method: 'POST',
									data: {},
									success: res => {
										if (res.data.success) {
											this.allorder = res.data.list
											uni.request({
												url: 'http://localhost:8484/ttwm/ttwm/addOrderFood',
												method: 'POST',
												data: {
													"orderIds": this.allorder[0].orderId,
													"foodIds": this.foodone.foodId,
													"sum": this.sum,
													"total":this.total
												},
												success: res => {
													uni.switchTab({
														url: "/pages/tab/order"
													})
												},
												fail: () => {},
												complete: () => {}
											});
										}
									},
									fail: () => {},
									complete: () => {}
								});
							}
						},
						fail: () => {},
						complete: () => {}
					});
				
			}else{
				for (var i = 0; i < this.allorder.length; i++) {
					if (this.oneuser.userId == this.allorder[i].userId && this.allorder[i].payment == '未支付') {
						this.user = false
						this.noPay = false
						this.id=this.allorder[i].orderId
						console.log(this.user);
						
							
						
						
					}	
				}
				
				if(this.user == true){
					uni.request({
						url: 'http://localhost:8484/ttwm/ttwm/addOrder',
						method: 'POST',
						data: {
							"userId": this.oneuser.userId,
							"payment": '未支付'
						},
						success: res => {
							if (res.data.success) {
								uni.request({
									url: 'http://localhost:8484/ttwm/ttwm/allOrder',
									method: 'POST',
									data: {},
									success: res => {
										if (res.data.success) {
											this.allorder = res.data.list
											uni.request({
												url: 'http://localhost:8484/ttwm/ttwm/addOrderFood',
												method: 'POST',
												data: {
													"orderIds": this.allorder[this.allorder.length-1].orderId,
													"foodIds": this.foodone.foodId,
													"sum": this.sum,
													"total":this.total
												},
												success: res => {
													uni.switchTab({
														url: "/pages/tab/order"
													})
												},
												fail: () => {},
												complete: () => {}
											});
										}
									},
									fail: () => {},
									complete: () => {}
								});
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}else{
					for (var i = 0; i < this.allorder.length; i++) {
						if(this.oneuser.userId == this.allorder[i].userId){
						if(this.noPay == false){
							uni.request({
								url: 'http://localhost:8484/ttwm/ttwm/addOrderFood',
								method: 'POST',
								data: {
									"orderIds": this.id,
									"foodIds": this.foodone.foodId,
									"sum": this.sum,
									"total":this.total
								},
								success: res => {
									uni.switchTab({
										url: "/pages/tab/order"
									})
								},
								fail: () => {},
								complete: () => {}
							});
							break
						}else{
							uni.request({
								url: 'http://localhost:8484/ttwm/ttwm/addOrder',
								method: 'POST',
								data: {
									"userId": this.oneuser.userId,
									"payment": '未支付'
								},
								success: res => {
									if (res.data.success) {
										uni.request({
											url: 'http://localhost:8484/ttwm/ttwm/allOrder',
											method: 'POST',
											data: {},
											success: res => {
												if (res.data.success) {
													this.allorder = res.data.list
													uni.request({
														url: 'http://localhost:8484/ttwm/ttwm/addOrderFood',
														method: 'POST',
														data: {
															"orderIds": this.id,
															"foodIds": this.foodone.foodId,
															"sum": this.sum,
															"total":this.total
														},
														success: res => {
															uni.switchTab({
																url: "/pages/tab/order"
															})
														},
														fail: () => {},
														complete: () => {}
													});
												}
											},
											fail: () => {},
											complete: () => {}
										});
									}
								},
								fail: () => {},
								complete: () => {}
							});
							break
						}
						
					}
					}
				}
			
			}
		}

	}



	},
	onLoad(id) {
		uni.getStorage({
			key: "userid",
			success: res => {
				this.oneuser = res.data
				
			}
		})
	
	// try {
	// 	this.foodid = uni.getStorageSync('foodid');
			// console.log(this.foodid);
	// } catch (e) {
		
	// }
		
		uni.getStorage({
			key:"foodid",
			success:res=>{
				this.foodid=res.data
			}
		})
		
		uni.request({
			url: 'http://localhost:8484/ttwm/ttwm/foodone',
			method: 'POST',
			data: {
				"foodId": this.foodid
				// "foodId": id.id
			},
			success: res => {

				if (res.data.success) {
					this.foodone = res.data.data
				}
			},
			fail: () => {},
			complete: () => {}
		});
		uni.request({
			url: 'http://localhost:8484/ttwm/ttwm/allOrder',
			method: 'POST',
			data: {},
			success: res => {
				if (res.data.success) {
					this.allorder = res.data.list
					
				}
			},
			fail: () => {},
			complete: () => {}
		});
	}
	}
</script>

<style>
	.lh {
		line-height: 70px;
		margin-left: 5px;
	}
</style>